package com.gxa.insurance.service.impl;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.mapper.CarBrandMapper;
import com.gxa.insurance.pojo.CarBrand;
import com.gxa.insurance.pojo.CarType;
import com.gxa.insurance.pojo.ThirdAdmin;
import com.gxa.insurance.service.CarBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
@Transactional(rollbackFor = Throwable.class)
public class CarBrandServiceImpl implements CarBrandService {
    @Autowired
    CarBrandMapper carBrandMapper;

    @Override
    public List<String> selectAllByname(String carname) throws Exception {
        if(carname==null){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        List<String> name1 = new ArrayList<>();
        List<CarBrand> carBrands = carBrandMapper.selectAllByname(carname);
        // 提取每个对象中的名字
        for (int i = 0; i < carBrands.size(); i++) {
            List<CarType> carTypes = carBrands.get(i).getCarTypes();
            for (CarType carType : carTypes) {
                name1.add(carType.getCarType());
            }
        }
        return  name1;
    }

    @Override
    public List<String> selectAll() throws BusinessException {
        List<String> name = new ArrayList<>();

        List<CarBrand> carBrands = carBrandMapper.selectAll();
        // 提取每个对象中的名字
        for (CarBrand carBrand : carBrands) {
            name.add(carBrand.getName());
        }
        return name;
    }
}
